home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 April / PCWorld_2008-04_cd.bin / temacd / pspad / pspad453inst_cz(2).exe / {app} / Script / JScript / ingkcpos.js next >
Encoding:
Text File  |  2007-11-21  |  15.4 KB  |  438 lines

  1. /*    
  2.     Provided under the MIT License
  3.     Copyright (c) July 2007 Damion Hankejh, d@ingk.com
  4.     ---
  5.     FILE:            ingkcpos.js
  6.     VERSION:        0.5
  7.     WEB:            www.sf.net/projects/ingkcpos
  8.  
  9.     DESCRIP:        PSPad Editor Extension to enhance 
  10.                     CTRL+(SHIFT)+RIGHT/LEFT cursor behavior.
  11.  
  12.                     Compatible/tested with PSPad versions:
  13.                     -    (full version) 4.5.2 (2240) - Windows XP/SP2 
  14.                     -    (beta release) 4.5.3 (2262) - Windows XP/SP2
  15.                     -    (beta release) 4.5.3 (2265) - Windows XP/SP2
  16.  
  17.     INSTALL:        1.    If a previous version has been installed, uninstall 
  18.                         it by deleting these files:
  19.                         
  20.                         ~\PSPad editor\Script\JScript\CTRL+SHIFT+RIGHT.js
  21.                         ~\PSPad editor\Script\JScript\CTRL+SHIFT+LEFT.js
  22.                         ~\PSPad editor\Script\JScript\CTRL+RIGHT.js
  23.                         ~\PSPad editor\Script\JScript\CTRL+LEFT.js
  24.  
  25.                     2.    Exit PSPad
  26.  
  27.                     3.    Place the this script (ingkcpos.js) in:
  28.                         ~\PSPad editor\Script\JScript
  29.  
  30.                     4.    Start PSPad
  31.  
  32.  
  33.     USE:            [ CTRL+RIGHT ] and [ CTRL+LEFT ] move the cursor to 
  34.                     the next right/left-context cursor stop.
  35.  
  36.                     [ CTRL+SHIFT+RIGHT/LEFT ] selects text with each
  37.                     cursor move.
  38.  
  39.     AUTHORS:        Original:    Damion Hankejh, d@ingk.com
  40.                     Updates:        AndyBridges, Michal Caplygin
  41.  
  42.     LICENSE:        MIT License
  43.                     www.opensource.org/licenses/mit-license.php
  44.  
  45.     ---
  46.     NOTES:        Behavior
  47.  
  48.                     -    Native positioning in PSPad yields cursor stops 
  49.                         indicated by "|" in:
  50.  
  51.                             Key combo                Cursor stops (PSPad)
  52.                             ---                        ---
  53.                             Original text            if (isSet($error) && $error != '') $text_block.= '<p>'.$error.'</p>';            
  54.                             CTRL+(SHIFT)+RIGHT    if (|isSet($|error) && $|error != '') $|text_block.= '<|p>'.$|error.'</|p>';|
  55.                             CTRL+(SHIFT)+LEFT        |if (|isSet($|error) && $|error != '') $|text_block.= '<|p>'.$|error.'</|p>';        
  56.  
  57.                     -    Enhanced positioning enabled with the ingkcpos 
  58.                         PSPad extension yields:
  59.  
  60.                             Key combo                Cursor stops (ingkcpos)
  61.                             ---                        ---
  62.                             Original text            if (isSet($error) && $error != '') $text_block.= '<p>'.$error.'</p>';
  63.                             CTRL+(SHIFT)+RIGHT    if |(|isSet|($|error|) |&& $|error |!= |'') $|text|_|block|.= |'<|p|>'.$|error|.'</|p|>';|
  64.                             CTRL+(SHIFT)+LEFT        |if |(|isSet|(|$|error|)| |&&| |$|error |!=| |'')| |$|text|_|block|.=| |'<|p|>'.$|error|.'</|p|>';| 
  65.  
  66.     ---
  67.     TODO:            Add Settings GUI Dialog
  68.  
  69.                     -    In some cases, it is useful to have cursor stops on specific characters (eg. TAB).  
  70.                         A settings GUI dialog accessible from the [Scripts > Ingkcpos] submenu would provide 
  71.                         convenient access to this as well as accepting input for additional cursor navigation
  72.                         settings.  Storing settings in the ~\PSPad editor\ directory would further improve
  73.                         a settings implementation. 
  74.  
  75.                     -    Access to white, alpha and delim regular expressions could also be added to a
  76.                         settings dialog.
  77.  
  78.                     Implement alternate cursor behavior models
  79.  
  80.                     -    Ingkcpos cursor behavior is modeled on (most similar to) UltraEdit/TextPad.
  81.                         Implementing alternate behavior models drawn from other editors or browsers would
  82.                         further customize cursor movement:
  83.  
  84.                         WinVi:                  if |(isSet($error) |&& |$error |!= |'') |$text_block.= |'<p>'.$error.'</p>';|
  85.                         Firefox:                if |(|isSet($|error) |&& |$|error |!= |'') |$|text_|block.= |'<|p>'.$|error.'</|p>';|
  86.                         Cold fusion(R):    if (|isSet($|error) && $|error != |'') $|text_block.= |'|<p>|'.$|error|.'|</p>|';|
  87.                         Cold Fusion(K):    |if (|isSet($|error) && $|error != |'') $|text_block.= |'|<p>|'.$|error.|'|</p>|';
  88.  
  89.     ---
  90.     DONE:            Unsaved Changes
  91.  
  92.                     -    Use ingkcpos to reposition the cursor causes the editor to flag the file as
  93.                         modified (indicated by a red triangle in the file tab, or by a diskette icon in the 
  94.                         status bar).  However, no changes whatsoever are made to the contents of the file.
  95.                         
  96.                         June 26, 2007 - AndyBridges killed this bug.  
  97.                         Thanks to his investigation, the "modified flag" bug appears to be related to the 
  98.                         use of 'ed.command("ecNextLineStart");' to position the cursor, so he replaced it 
  99.                         with calls to ed.caretX() and ed.caretY().
  100.                         
  101.                     Non-ASCII Character Support
  102.                     
  103.                     -    Cursor movement stops upon encountering non-ASCII characters.
  104.                     
  105.                         July 16, 2007 - Michal Caplygin
  106.                         Altered the alpha regular expression to enable cursor movement when editing files 
  107.                         containing non-ASCII characters.
  108.  
  109. */
  110.  
  111. var module_name    = "Ingkcpos";
  112. var module_ver        = "0.5";
  113. var module_title    = 
  114.     "CTRL+(SHIFT)+LEFT/RIGHT cursor navigator";
  115. var white =    /\s/;                                                                    // Specify whitespaces, alphanumerics and delimiters
  116. var alpha = /[^\s\~\!\@\#\$\%\^\&\*\(\)\_\+\{\}\|\:\<\>\?\`\-\=\[\]\\\;\,\.\/\"\']/;    
  117. var delim =    /[\~,\!,\@,\#,\$,\%,\^,\&,\*,\(,\),\_,\+,\{,\},\|,\:,\<,\>,\?,\`,\Æ,\-,\û,\ù,\=,\[,\],\\,\;,\,,\.,\/,\",\']/;
  118.  
  119.  
  120. function Init() 
  121. {
  122.     /* Adds this extension to the PSPad Script menu
  123.         and assigns hot keys.
  124.     */
  125.  
  126.     addMenuItem("CTRL+RIGHT","Ingkcpos","ueCR","CTRL+RIGHT");
  127.     addMenuItem("CTRL+LEFT","Ingkcpos","ueCL","CTRL+LEFT");            
  128.     addMenuItem("CTRL+SHIFT+RIGHT","Ingkcpos","ueCSR","CTRL+SHIFT+RIGHT");
  129.     addMenuItem("CTRL+SHIFT+LEFT","Ingkcpos","ueCSL","CTRL+SHIFT+LEFT");
  130.     addMenuItem("-","Ingkcpos","","");                                        // Menu divider
  131.     addMenuItem("Edit this script","Ingkcpos","editMe");                // Edit this script in PSPad
  132.      addMenuItem("-","Ingkcpos","","");                                        // Menu divider
  133.     addMenuItem("About","Ingkcpos","about","");                            // About this extension
  134.     return;
  135. }
  136.  
  137.  
  138. function editMe() 
  139. {
  140.     /*    Edit this script in PSPad.
  141.     */
  142.     
  143.     oEdit = newEditor();
  144.     oEdit.openFile(moduleFileName(module_name));
  145. }
  146.  
  147.  
  148. function constructEditor() 
  149. {
  150.     /* Construct new editor object, get line text and
  151.         set trim.
  152.     */
  153.  
  154.     ed = newEditor();                                                                // Construct new editor object.
  155.     try {
  156.         ed.assignActiveEditor();                                                // Assign active editor window.
  157.         
  158.     } catch(e) {
  159.         echo("Failed to assign active editor window.");
  160.         return;                                                            
  161.     }
  162.     line = ed.lineText();                                                        // Get current text line.
  163.     curx = ed.caretX();                                                            // Current cursor column position.
  164.     cury = ed.caretY();                                                            // Current cursor row position.
  165.     trim = 0;                                                                        // Tally of trimmed characters.
  166.     return;
  167. }
  168.  
  169.  
  170. function ueCR() 
  171. {
  172.     /* UltraEdit/TextPad [ CTRL+RIGHT ] cursor behavior:
  173.         move to next right-context cursor stop.
  174.     */
  175.  
  176.     constructEditor();                                                            // Construct editor obj, etc.
  177.     var posx = curx - 1;                                                            // String pos begins at 0, cursor begins at column 1.
  178.     if (curx >= line.length) {                                                    // IF cursor at end-of-line,
  179.         //ed.command("ecNextLineStart");                                        // move to start of next line.
  180.         curx = 0;
  181.         cury++;
  182.         
  183.     } else {                                                                            // ELSE find next CTRL+RIGHT stop
  184.         var c = line.charAt((posx + trim));
  185.         if (c.match(white)) {                                                    // CURSOR ON WHITE SPACE:
  186.             trim += 
  187.                 count((line.slice(posx + trim)), white);                    // move over white spaces,
  188.             var c = line.charAt((posx + trim));
  189.             if (c == '$') {                                                        // move over $ trailing whites.
  190.                 trim++;
  191.             }
  192.  
  193.         } else 
  194.         if (c.match(delim)) {                                                    // CURSOR ON DELIMITER:
  195.             trim += 
  196.                 count((line.slice(posx + trim)), delim);                    // move over delimiters,
  197.             c = line.charAt((posx + trim));
  198.             trim +=
  199.                 count((line.slice(posx + trim)), white);                    // move over white space trailing delims,
  200.             c = line.charAt((posx + trim));
  201.             if (c == '$') {                                                        // move over $ trailing whites.
  202.                 trim++;
  203.             }
  204.  
  205.         } else 
  206.         if (c.match(alpha)) {                                                    // CURSOR ON ALPHANUMERIC:
  207.             trim +=
  208.                 count((line.slice(posx + trim)), alpha);                    // move over alphanumerics,
  209.             c = line.charAt((posx + trim));
  210.             trim +=
  211.                 count((line.slice(posx+trim)), white);                        // move over white space trailing alphas,
  212.             c = line.charAt((posx + trim));
  213.             if (c == '$') {                                                        // move over $ trailing whites.
  214.                 trim++;
  215.             }
  216.         }
  217.         curx += trim;                                                                // Account for trimmed chars.
  218.     } // IF (cursor at end of line)    
  219.     ed.caretX(curx);                                                                // Reposition cursor column.
  220.     ed.caretY(cury);                                                                // Reposition cursor row.
  221.     return;
  222. }
  223.  
  224.  
  225. function ueCL() 
  226. {
  227.     /* UltraEdit/TextPad [ CTRL+LEFT ] cursor behavior: 
  228.         move to next left-context cursor stop.
  229.     */
  230.  
  231.     constructEditor();                                                            // Construct editor obj, etc.
  232.     var posx = line.length - curx + 1;                                        // Column position in string.
  233.     if (curx == 1) {                                                                // IF cursor at start-of-line,
  234. //        ed.command("ecLeft");                                                    // move to end of previous line. PM: this command was replaced by the two commands bellow
  235.         if (cury > 1) {                                                            // PM: if not on first line
  236.             ed.command("ecUp");                                                    // PM: move cursor up
  237.             ed.command("ecLineEnd");                                            // PM: and to the end of line
  238.         }
  239.     } else if (curx > line.length+1) {                                            // PM: if beyond end of line
  240.         ed.command("ecLineEnd");                                                // PM: move cursor to the end of line
  241.     } else {                                                                            // ELSE find next CTRL+LEFT stop
  242.         line = line.reverse();                                                        // Reverse the text to leverage CTRL-RIGHT logic. PM: moved from beginning of the function to here, no need to run it every time
  243.         var c = line.charAt((posx + trim));
  244.         if (c == '$') {                                                            // CURSOR ON $:
  245.             trim++;                                                                    // move over $.
  246.  
  247.         } else
  248.         if (c.match(white)) {                                                    // CURSOR ON WHITE SPACE:
  249.             trim +=
  250.                 count((line.slice(posx + trim)), white);                    // move over white spaces,
  251.             trim +=
  252.                 count((line.slice(posx + trim)), alpha);                    // move over alphas.
  253.             var c = line.charAt((posx + trim));
  254.             
  255.         } else 
  256.         if (c.match(delim)) {                                                    // CURSOR ON DELIMITER:
  257.             trim +=
  258.                 count((line.slice(posx + trim)), delim);                    // move over delimiters,
  259.             
  260.         } else 
  261.         if (c.match(alpha)) {                                                    // CURSOR ON ALPHANUMERIC:
  262.             trim +=
  263.                 count((line.slice(posx + trim)), alpha);                    // move over alphanumerics,
  264.         }
  265.         curx = curx - trim;                                                        // Account for trimmed chars.
  266.         ed.caretX(curx);                                                            // Reposition cursor.
  267.     } // IF (cursor at start-of-line)
  268.     return;
  269. }
  270.  
  271.  
  272. function ueCSR() 
  273. {
  274.     /* UltraEdit/TextPad [ CTRL+SHIFT+RIGHT ] cursor 
  275.         behavior: move and select tax through next 
  276.         right-context cursor stop.
  277.     */
  278.  
  279.     constructEditor();                                                            // Construct editor obj, etc.
  280.     var posx =    curx - 1;                                                        // String pos begins at 0, cursor begins at column 1.
  281.  
  282.     if (curx >= line.length) {                                                    // If cursor at end-of-line,
  283. //        ed.command("ecSelRight");                                                // continue selection to start of next line. PM: command replaced
  284.         ed.command("ecSelDown");                                                // PM: Move selection to the next line
  285.         ed.command("ecSelLineStart");                                            // PM: Move selection to the begging of the line
  286.     } else {                                                                            // ELSE find next CTRL+RIGHT stop
  287.         var c = line.charAt((posx + trim));
  288.         if (c.match(white)) {                                                    // CURSOR ON WHITE SPACE:
  289.             trim +=
  290.                 count((line.slice(posx + trim)), white);                    // move over white spaces,
  291.             var c = line.charAt((posx + trim));
  292.             if (c == '$') {                                                        // move over $ trailing whites.
  293.                 trim++;
  294.             }
  295.  
  296.         } else 
  297.         if (c.match(delim)) {                                                    // CURSOR ON DELIMITER:
  298.             trim +=
  299.                 count((line.slice(posx + trim)), delim);                    // move over delimiters,
  300.             c = line.charAt((posx + trim));
  301.             trim +=
  302.                 count((line.slice(posx + trim)), white);                    // move over white space trailing delims,
  303.             c = line.charAt((posx + trim));
  304.             if (c == '$') {                                                        // move over $ trailing whites.
  305.                 trim++;
  306.             }
  307.  
  308.         } else 
  309.         if (c.match(alpha)) {                                                    // CURSOR ON ALPHANUMERIC:
  310.             trim +=
  311.                 count((line.slice(posx + trim)), alpha);                    // move over alphanumerics,
  312.             c = line.charAt((posx + trim));
  313.             trim +=
  314.                 count((line.slice(posx + trim)), white);                    // move over white space trailing alphas,
  315.             c = line.charAt((posx + trim));
  316.             if (c == '$') {                                                        // move over $ trailing whites.
  317.                 trim++;
  318.             }
  319.         }
  320.         for (n=0; n < trim; n++) {                                                // Position cursor by
  321.             ed.command("ecSelRight");                                            // selecting each trim char.
  322.         }
  323.     }
  324.     return;
  325. }
  326.  
  327.  
  328. function ueCSL() 
  329. {
  330.     /* UltraEdit/TextPad [ CTRL+SHIFT+LEFT ] cursor 
  331.         behavior: move and select tax through next 
  332.         left-context cursor stop.
  333.     */
  334.  
  335.     constructEditor();                                                            // Construct editor obj, etc.
  336.     var posx = line.length - curx + 1;                                        // Column position in string.
  337.     if (curx == 1) {                                                                // If cursor at start-of-line,
  338. //        ed.command("ecSelLeft");                                                // continue selection to end of previous line. PM: Command replaced
  339.         if (cury > 1) {                                                            // PM: If not on first line
  340.             ed.command("ecSelUp");                                                // PM: move selection on line up
  341.             ed.command("ecSelLineEnd");                                            // PM: and to the end of line
  342.         }
  343.     } else if (curx > line.length+1) {                                            // PM: if beyond end of line
  344.         ed.command("ecSelLineEnd");                                                // PM: move selection to the end of line
  345.     } else {                                                                            // ELSE find next CTRL+LEFT stop
  346.         line = line.reverse();                                                        // Reverse the text so as to reuse CTRL-RIGHT logic. PM: moved from beginning of the function to here, no need to run it every time
  347.         var c = line.charAt( (posx + trim) );
  348.         if (c == '$') {                                                            // CURSOR ON $:
  349.             trim++;                                                                    // move over $.
  350.  
  351.         } else
  352.         if (c.match(white)) {                                                    // CURSOR ON WHITE SPACE:
  353.             trim +=
  354.                 count((line.slice(posx + trim)), white);                    // move over white spaces,
  355.             trim +=
  356.                 count((line.slice(posx + trim)), alpha);                    // move over alphas.
  357.             var c     = line.charAt( (posx + trim) );
  358.  
  359.         } else 
  360.         if (c.match(delim)) {                                                    // CURSOR ON DELIMITER:
  361.             trim +=
  362.                 count((line.slice(posx + trim)), delim);                    // move over delimiters,
  363.  
  364.         } else 
  365.         if (c.match(alpha)) {                                                    // CURSOR ON ALPHANUMERIC:
  366.             trim +=
  367.                 count((line.slice(posx + trim)), alpha);                    // move over alphanumerics,
  368.         }
  369.         for (n=0; n < trim; n++) {                                                // Position cursor by
  370.             ed.command("ecSelLeft");                                            // selecting each trim char.
  371.         }
  372.     } // IF (cursor at start-of-line)
  373.     return;
  374. }
  375.  
  376.  
  377. function about() 
  378. {
  379.     /*    Extension description, how to use, license, 
  380.         open source project web address, and developer 
  381.         credits.     
  382.     */
  383.  
  384.     echo(
  385.         "\n" + module_name + " " + module_ver + "\n\n" +
  386.         "_________________\n" +
  387.         "DESCRIPTION\n\n" +
  388.         "   PSPad Editor extension to enhance native\n" +
  389.         "   cursor behavior.\n" +
  390.         "_________________\n" +
  391.         "USE\n\n" +
  392.         "   [ CTRL+RIGHT ] and [ CTRL+LEFT ] move the\n" + 
  393.         "   cursor to the next right/left-context stop.\n\n" +
  394.         "   [ SHIFT+CTRL+RIGHT ] and [ SHIFT+CTRL+LEFT ]\n" +
  395.         "   select text with each move.\n" +
  396.         "_________________\n" +
  397.         "CREDITS\n\n" +
  398.         "   www.sf.net/projects/ingkcpos\n\n" +
  399.         "   MIT License\n" +
  400.         "   Copyright (c) July 2007 Damion HΣnkejh, ingk.com\n" +
  401.         "   www.opensource.org/licenses/mit-license.php\t\t\n\n" +
  402.         "   Contributing developer(s): AndyBridges, Michal Caplygin\n"
  403.     );
  404.     return;
  405. }
  406.  
  407.  
  408. function count(text, charType) 
  409. {
  410.     /* Count consecutive regex:charType characters
  411.         in string:text.
  412.     */
  413.  
  414.     var n = 0;
  415.     var c = text.charAt(n);
  416.     while (c.match(charType)) {                                                // While char is regex:charType
  417.         n++;                                                                            // move cursor forward,
  418.         c = text.charAt(n);                                                        // get next char.
  419.     }
  420.     return n;
  421. }
  422.  
  423.  
  424. String.prototype.reverse = 
  425.     function() 
  426.     {
  427.         /* Return the string reversed.
  428.         */
  429.  
  430.         var s = "";
  431.         var n = this.length;
  432.         while (n > 0) {
  433.             s += this.substring((n - 1), n);
  434.             n--;
  435.         }
  436.         return s;
  437.     }
  438.